<template>
  <el-dialog
    @close="sqbjCancel('sqbjRuleForm')"
    :close-on-click-modal="false"
    title="申请报价"
    :visible.sync="sqbjVisible"
    width="600px"
  >
    <div class="scroll-view">
      <el-form
        :model="sqbjRuleForm"
        :rules="sqbjRules"
        ref="sqbjRuleForm"
        label-position="top"
      >
        <el-form-item label="预计开工日期" prop="start_date">
          <el-date-picker
            class="input-box"
            v-model="sqbjRuleForm.start_date"
            type="date"
            placeholder="选择日期"
            clearable
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptionsEnd"
            @change="sqbjDataFunc($event, 'start')"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预计完工日期" prop="end_date">
          <el-date-picker
            class="input-box"
            v-model="sqbjRuleForm.end_date"
            type="date"
            :disabled="!sqbjRuleForm.start_date"
            placeholder="选择日期"
            clearable
            
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions_"
            @change="sqbjDataFunc($event, 'end')"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          v-if="sqbjRuleForm.expect_duration_days"
          label="预计工期"
          prop="expect_duration_days"
        >
          <el-input
            class="input-box"
            v-model="sqbjRuleForm.expect_duration_days"
            disabled
            ><span slot="suffix">天</span></el-input
          >
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            :rows="4"
            class="input-box"
            v-model="sqbjRuleForm.remark"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer">
      <el-button
        type="primary"
        @click="sqbjSubmit('sqbjRuleForm')"
        :disabled="diaLoading"
        :loading="diaLoading"
        >提交</el-button
      >
      <el-button @click="sqbjCancel('sqbjRuleForm')">关闭</el-button>
    </span>
  </el-dialog>
</template>
<script>
import projectApi from "@/api/projectApi";
import companyApi from "@/api/companyApi";
import adminApi from "@/api/adminApi";
import { getUserInfo } from "@/utils/auth";
import foremanApi from "@/api/foremanApi";

export default {
  name: "",
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false,
      required: false,
    },
    project_id: {
      required: false,
    },
  },
  data() {
    return {
      sqbjVisible: false,
      sqbjRuleForm: {
        start_date: "",
        end_date: "",
        expect_duration_days: "",
        remark: "",
      },
      sqbjRules: {
        start_date: [
          { required: true, message: "请选择开工日期", trigger: "change" },
        ],
        end_date: [
          { required: true, message: "请选择完工日期", trigger: "change" },
        ],
      },
      diaLoading: false,
      pickerOptionsStart: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      pickerOptionsEnd: {
        disabledDate(time) {
          return time.getTime() < Date.now();
        },
      },
      pickerOptions_: {},
    };
  },
  watch: {
    addDialogVisible(val1, val2) {
      this.sqbjVisible = this.addDialogVisible;
    },
  },
  computed: {},
  mounted() {},
  methods: {
    compareDates(date1, date2) {
      return date1.getTime() - date2.getTime() < 0;
    },
    // 申请报价
    sqbjCancel(formName) {
      this.sqbjRuleForm = {
        start_date: "",
        end_date: "",
        expect_duration_days: "",
        remark: "",
      };
      this.$nextTick(() => {
        this.$refs[formName].clearValidate();
        this.$emit("onClose", true);
      });
    },
    sqbjSubmit(formName) {
      let sqbjRuleForm = this.sqbjRuleForm;
      console.log(sqbjRuleForm, "上传报价参数");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.diaLoading = true;
          projectApi
            .applyQuotation({
              project_id: this.project_id,
              start_date: sqbjRuleForm.start_date,
              end_date: sqbjRuleForm.end_date,
              expect_duration_days: sqbjRuleForm.expect_duration_days,
              remark: sqbjRuleForm.remark,
            })
            .then((res) => {
              console.log(res, "提交报价申请");
              this.diaLoading = false;
              if (res.code == 200) {
                this.$emit("onSuccess", true);
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    sqbjDataFunc(e, type) {
      let sqbjRuleForm = this.sqbjRuleForm;
      if (type == "start") {
        if (sqbjRuleForm.start_date) {
          this.pickerOptions_ = {
            disabledDate(time) {
              return (
                time.getTime() < new Date(sqbjRuleForm.start_date).getTime()
              );
            },
          };
        } else {
          sqbjRuleForm.end_date = "";
        }
      }
      if (sqbjRuleForm.start_date && sqbjRuleForm.end_date) {
        const date1 = new Date(sqbjRuleForm.start_date);
        const date2 = new Date(sqbjRuleForm.end_date);
        if (!this.compareDates(date1, date2)) {
          this.$message.warning("完工日期需晚于开工日期");
          type == "start"
            ? (sqbjRuleForm.start_date = "")
            : (sqbjRuleForm.end_date = "");
          sqbjRuleForm.expect_duration_days = "";
          this.sqbjRuleForm = sqbjRuleForm;
          return;
        }
        sqbjRuleForm.expect_duration_days = this.daysBetween(date1, date2) + 1;
      } else {
        sqbjRuleForm.expect_duration_days = "";
      }
      this.sqbjRuleForm = sqbjRuleForm;
    },
    daysBetween(date1, date2) {
      const oneDay = 24 * 60 * 60 * 1000; // 每天的毫秒数
      const firstTime = date1.getTime();
      const secondTime = date2.getTime();

      return Math.round(Math.abs((firstTime - secondTime) / oneDay));
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>